package cn.mxb.logic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysVillageService;

@SuppressWarnings({ "serial", "rawtypes" })
public class SysVillageAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/floor/villageList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/floor/villageAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/floor/villageUpdate.jsp";
	
	private SysVillage sysVillage;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	
	private Result<SysVillage> pageResult;
	
	private List<SysVillage> SysVillageAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();  //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();  //镇名
	
	
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @return_type: String
	 */
	public String add(){
		try{
			System.out.println("add");
			sysVillageService.add(sysVillage);
			sysVillage = null;
			return openList();
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			pageResult = sysVillageService.find(sysVillage, getPage(), getRow());
			for(SysVillage sysVillage : pageResult.getData()){
				if(null != sysVillage.getCountId()  && !"".equals(sysVillage.getCountId())){
					countNameMap.put(sysVillage.getCountId(),sysCountService.findByProperty("countId", sysVillage.getCountId()+"").getCountName());
				}
				if(null != sysVillage.getTownId() && !"".equals(sysVillage.getTownId())){
					townNameMap.put(sysVillage.getTownId(),sysTownService.findByProperty("sysTownId", sysVillage.getTownId()+"").getSysTownName());
				}
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		sysVillageService.delete(sysVillage.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		sysVillage =  sysVillageService.findByProperty("id",sysVillage.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		sysVillageService.add(sysVillage);
		sysVillage = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return sysVillage;
	}
	@Override
	public void prepare() throws Exception {
		if(sysVillage ==null){
			sysVillage = new SysVillage();
		}
	}


	public SysVillage getSysVillage() {
		return sysVillage;
	}

	public void setSysVillage(SysVillage sysVillage) {
		this.sysVillage = sysVillage;
	}

	public Result<SysVillage> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysVillage> pageResult) {
		this.pageResult = pageResult;
	}

	
	public List<SysVillage> getSysVillageAllList() {
		return SysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> SysVillageAllList) {
		this.SysVillageAllList = SysVillageAllList;
	}


	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}


	

	
	
}
